package com.google.code.vannevar.client.ui.builders;

import com.google.code.vannevar.client.ui.listeners.LogonListener;
import com.google.code.vannevar.client.ui.listeners.CancelListener;
import com.google.gwt.user.client.Cookies;
import com.gwtext.client.core.RegionPosition;
import com.gwtext.client.data.SimpleStore;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Component;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.form.Checkbox;
import com.gwtext.client.widgets.form.ComboBox;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.layout.BorderLayout;
import com.gwtext.client.widgets.layout.BorderLayoutData;

public class LoginWindowBuilder implements Builder {

    public Component get() {
        
        Window loginWindow = new Window("Login", 250, 200, true, false);
        
        loginWindow.setId("login-window");
        loginWindow.setLayout(new BorderLayout());
        
        loginWindow.add(getForm(), new BorderLayoutData(RegionPosition.CENTER));
        
        return loginWindow;
    }

    private FormPanel getForm() {
        
        FormPanel form = new FormPanel();
        
        form.setId("form");
        form.setWidth(225);
        form.setBodyBorder(false);
        
        form.add(getURL());
        form.add(getLogin());
        form.add(getPassword());
        form.add(getRemember());
        form.addButton(getLogon());        
        form.addButton(getCancel());
        
        return form;
    }

    private ComboBox getURL() {
        
        ComboBox url = new ComboBox("URL", "url");
        
        url.setAllowBlank(false);
        url.focus();
        url.setMinChars(1);  
        url.setDisplayField("url");
        url.setMode(ComboBox.LOCAL);
        url.setEmptyText("Enter URL");
        url.setLoadingText("Searching...");
        url.setTypeAhead(true);
        url.setSelectOnFocus(true);
        
        url.setStore(new SimpleStore("url",
                Cookies.getCookie("vannevar.login.url").split(",")));
        
        return url;
    }

    private TextField getLogin() {
        
        TextField login = new TextField("Login", "login");
        
        login.setAllowBlank(false);
        
        return login;
    }

    private TextField getPassword() {
        
        TextField password = new TextField("Password", "password");
        
        password.setAllowBlank(false);
        password.setPassword(true);
        
        return password;
    }

    private Checkbox getRemember() {
        
        Checkbox remember = new Checkbox("Remember me");
        
        return remember;
    }

    private Button getLogon() {
        
        Button logon = new Button("Logon", new LogonListener());
        
        return logon;
    }

    public Button getCancel() {
        
        Button cancel = new Button("Cancel", new CancelListener());
        
        return cancel;
    }
}
